package org.utils.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.utils.common.InitException;
import org.utils.ui.actioncommand.ActionCommand;
import org.utils.ui.actioncommand.ActionCommandManager;

/**
 * @author Manuel EVENO
 */
public class ButtonPanel extends JPanel
{
	private static final long serialVersionUID = 8797635095709475309L;
	private LayoutManager layout = null;
	private ArrayList buttons = new ArrayList();
	private Dimension maxButtonSize = null;
	/**
	 * Constructor for ButtonPanel.
	 */
	public ButtonPanel() throws InitException
	{
		super();
		try
		{
			jbInit();
		}
		catch (Exception e)
		{
			throw new InitException(e);
		}
	}

	/**
	 * Method jbInit.
	 */
	private void jbInit() throws Exception
	{
		
	}
	/**
	 * Returns the layout.
	 * @return LayoutManager
	 */
	public LayoutManager getLayout()
	{
		return layout;
	}

	/**
	 * Sets the layout (If no layout is added, a default java.awt.FlowLayout is created).
	 * @param layout The layout to set
	 */
	public void setLayout(LayoutManager pLayout)
	{
		this.layout = pLayout;
	}

	public void addButton(AbstractButton button)
	{
		Logger.getLogger(getClass()).debug("Adding AbstractButton");
		buttons.add(button);
		if (maxButtonSize == null)
		{
			 maxButtonSize = button.getPreferredSize();
			 return ;
		}
		maxButtonSize = (button.getPreferredSize().getWidth() < maxButtonSize.getWidth()) ? maxButtonSize : button.getPreferredSize();
	}

	public void addButton(AbstractButton button, ActionCommand command)
	{
		Logger.getLogger(getClass()).debug("Adding AbstractButton with ActionCommand");
		buttons.add(button);
		ActionCommandManager.getInstance().registerActionCommand(command);
		if (maxButtonSize == null)
		{
			 maxButtonSize = button.getPreferredSize();
			 return ;
		}
		maxButtonSize = (button.getPreferredSize().getWidth() < maxButtonSize.getWidth()) ? maxButtonSize : button.getPreferredSize();
	}
	
	public void addActionListener(ActionListener listener)
	{
		Logger.getLogger(getClass()).debug("Adding ActionListener");
		for (int i = 0; i < buttons.size(); i++)
		{
			AbstractButton c = (AbstractButton) buttons.get(i);
			c.addActionListener(listener);
		}
	}
	
	/**
	 * This methods has to be called when all elements have been added (layout, buttons).
	 * Until it has been called nothing has been added to the panel and nothing will appear
	 * on screen. If no layout is added, a default java.awt.FlowLayout is created
	 */
	public void buildPanel()
	{
		if (layout == null)
			layout = new FlowLayout();
		Logger.getLogger(getClass()).debug("Setting LayoutManager ("+layout.getClass().getName()+")");
		setLayout(layout);
		for (int i = 0; i < buttons.size(); i++)
		{
			AbstractButton c = (AbstractButton) buttons.get(i);
			Logger.getLogger(getClass()).debug("Setting button size: "+maxButtonSize.getWidth()+'x'+maxButtonSize.getHeight());
			c.setPreferredSize(maxButtonSize);
			Logger.getLogger(getClass()).debug("Adding button \""+c.getText()+"\" to panel");
			add(c);
		}
	}
}
